﻿using CodeGenerator.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

namespace CodeGenerator.Common
{
    public class DbToCS
    {
        private const string cachekeystring = "_dbtype_cache_";
        public static readonly string DbTypePath = (Application.StartupPath + "/Config/dbtype.xml");

        public static List<ColumnInfo> DbtoCSColumns(List<ColumnInfo> columns, string dbType)
        {
            Dictionary<string, string> dictionary = loadType();
            foreach (ColumnInfo info in columns)
            {
                try
                {
                    if (((info.TypeName.Trim().ToLower() == "char") && (info.Length == "36")) && ((dbType == "MySql") || (dbType == "Oracle")))
                    {
                        info.TypeName = dictionary["uniqueidentifier"];
                    }
                    else if (((info.TypeName.Trim().ToLower() == "tinyint") && (info.Length == "1")) && (dbType == "MySql"))
                    {
                        info.TypeName = dictionary["bit"];
                    }
                    else
                    {
                        info.TypeName = dictionary[info.TypeName.Trim().ToLower()];
                    }
                }
                catch
                {
                    info.TypeName = "object";
                }
                if (((!info.IsIdentity && !info.IsPrimaryKey) && info.IsColumnNull) && ((!info.TypeName.Equals("string") && !info.TypeName.Equals("object")) && !info.TypeName.Equals("byte[]")))
                {
                    info.TypeName = info.TypeName + "?";
                }
            }
            return columns;
        }

        private static Dictionary<string, string> loadType()
        {
            Dictionary<string, string> dictionary = null;
            if (dictionary == null)
            {
                dictionary = new Dictionary<string, string>();
                XmlDocument document = new XmlDocument();
                document.Load(DbTypePath);
                XmlNodeList list = document.SelectNodes("//type");
                if ((list == null) || (list.Count <= 0))
                {
                    return dictionary;
                }
                foreach (XmlNode node in list)
                {
                    XmlAttribute attribute = node.Attributes["dbtype"];
                    if (attribute != null)
                    {
                        string key = attribute.Value.Trim().ToLower();
                        if (!dictionary.ContainsKey(key))
                        {
                            XmlAttribute attribute2 = node.Attributes["cstype"];
                            if (attribute2 != null)
                            {
                                dictionary.Add(key, attribute2.Value);
                            }
                        }
                    }
                }
            }
            return dictionary;
        }
    }
}
